import { SUCCESS_CODE } from '@/constants'

const timeout = 1000

const List: {
  account: string
  password: string
  role: string
  roleId: string
  permissions: string | string[]
}[] = [
  {
    account: 'kyle',
    password: 'sD4Ri2o$ph^dRt*TcE',
    role: 'admin',
    roleId: '1',
    permissions: ['*.*.*']
  }
]

export default [
  // 列表接口
  {
    url: '/mock/user/list',
    method: 'get',
    response: ({ query }) => {
      const { account, pageIndex, pageSize } = query

      const mockList = List.filter((item) => {
        if (account && item.account.indexOf(account) < 0) return false
        return true
      })
      const pageList = mockList.filter(
        (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
      )

      return {
        code: SUCCESS_CODE,
        data: {
          total: mockList.length,
          list: pageList
        }
      }
    }
  },
  // 登录接口
  {
    url: '/api/v1/user/login.do',
    method: 'post',
    timeout,
    response: ({ body }) => {
      const data = body
      let hasUser = false
      for (const user of List) {
        if (user.account === data.account && user.password === data.password) {
          hasUser = true
          return {
            code: SUCCESS_CODE,
            data: user
          }
        }
      }
      if (!hasUser) {
        return {
          code: 500,
          message: '账号或密码错误'
        }
      }
    }
  },
  // 退出接口
  {
    url: '/mock/user/loginOut',
    method: 'get',
    timeout,
    response: () => {
      return {
        code: SUCCESS_CODE,
        data: null
      }
    }
  },
  // 用户信息接口
  {
    url: '/api/v1/user/info.do',
    method: 'post',
    timeout,
    response: () => {
      return {
        rspCode: 0,
        rspMsg: "",
        data: {
          id: 1,
          account: "bnb",
          ctime: "2025-01-01 00:00:00",
          roleid: 1,
          rolename: "管理员",
          status: 0,
          remarks: "管理员",
          privs: [
            {
              id: 1,
              name: "仪表盘",
              url: "/dashboard"
            },
            {
              id: 2,
              name: "人员管理",
              url: "/user"
            },
            {
              id: 3,
              name: "分组管理",
              url: "/group"
            },
            {
              id: 4,
              name: "操手管理",
              url: "/operator"
            }
          ]
        }
      }
    }
  }
]
